﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Controles.Principal
{
    public partial class ucControlFecha : System.Web.UI.UserControl
    {
        protected String DtFechaNula;
        public string NombreSessionHerramienta
        {
            get { return hidCFSessionHerramienta.Value; }
            set { hidCFSessionHerramienta.Value = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            btnFecha.Culture = ("es-PE");
            btnFecha.BlankFieldText = "Limpiar Fecha";
            btnFecha.Format = "dd mm yyyy";
        }

        public string Ancho
        {
            set { btnFecha.Width = Unit.Pixel(Convert.ToInt32(value)); }
        }

        public bool AutoPostback
        {
            get { return Convert.ToBoolean(hidFechaAutoPostBack.Value); }
            set
            {
                if (value == true)
                {
                    SetCalendarPostback(btnFecha);
                    hidFechaAutoPostBack.Value = "1";
                }
                else
                {
                    hidFechaAutoPostBack.Value = "0";
                }
            }
        }

        public String Fecha
        {

            get
            {
                if (!string.IsNullOrEmpty(btnFecha.SelectedDate))
                {
                    return btnFecha.SelectedDate.ToString();
                }
                else
                {
                    return DtFechaNula;
                }

            }
            set
            {
                if (value != DtFechaNula)
                {
                    btnFecha.SelectedDate = Convert.ToString(value);
                }
                else
                {
                    btnFecha.SelectedDate = "";
                }

                //txtPRFecha.VisibleDate = Value
            }
        }

        public bool Enabled
        {
            get { return btnFecha.Enabled; }
            set
            {
                btnFecha.Enabled = value;
                txtFecha.Enabled = value;
            }
        }

        private void SetCalendarPostback(RJS.Web.WebControl.PopCalendar PopCalendarX)
        {
            System.Text.StringBuilder _s = new System.Text.StringBuilder();
            PopCalendarX.ClientScriptOnDateChanged = PopCalendarX.ClientID + "_DateChanged";
            _s.Append(Convert.ToChar(13));
            _s.Append("<script type='text/javascript'>");
            _s.Append(Convert.ToChar(13));
            _s.Append("<!--");
            _s.Append(Convert.ToChar(13));
            _s.Append("function " + PopCalendarX.ClientID + "_DateChanged(_TextBox,_PopCal)");
            _s.Append(Convert.ToChar(13));
            _s.Append("{");
            _s.Append(Convert.ToChar(13));
            _s.Append("   eval(\"" + Page.GetPostBackEventReference(PopCalendarX, "9999x99x99") + "\".replace('9999x99x99',_TextBox.value))");
            _s.Append(Convert.ToChar(13));
            _s.Append("}");
            _s.Append(Convert.ToChar(13));
            _s.Append("// -->");
            _s.Append(Convert.ToChar(13));
            _s.Append("</script>");
            Page.RegisterClientScriptBlock(PopCalendarX.ClientID + "_DateChanged", _s.ToString());
        }
    }
}